package com.mago.tis.daoImpl;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mago.tis.bean.TradersBean;
import com.mago.tis.dao.ITraderDao;
import com.mago.tis.pojo.Tistit;
import com.mago.tis.utility.ApplicationException;
import com.mago.tis.utility.DBConstant;
import com.mago.tis.utility.ExceptionMessage;
import com.mago.tis.utility.QueryConstant;

public class TraderDao extends BaseDao implements ITraderDao {

	public boolean saveTrader(Tistit tit) throws ApplicationException {

		// Prepare the parameters
		List lParamList = new ArrayList();
		boolean isSaved = false;
		int pk = 0;
		if (tit != null) {
			try {
				pk = getPrimaryKey("COMPANY_ID", "tistit");
				lParamList.add(0, pk);
				lParamList.add(1, tit.getCompanyName());
				lParamList.add(2, tit.getAddress());
				lParamList.add(3, null);
				lParamList.add(4, null);
				lParamList.add(5, 0);
				lParamList.add(6, tit.getContactNo());
				lParamList.add(7, tit.getEmailId());
				lParamList.add(8, tit.getDesc());
				lParamList.add(9, DBConstant.IS_ACTIVE_YES);
				lParamList.add(10, new Date(System.currentTimeMillis()));
				lParamList.add(11, new Date(System.currentTimeMillis()));
				
				lParamList.add(12, tit.getMobileNo());
				lParamList.add(13, tit.getPanNo());
				
				lParamList.add(14, null);
				lParamList.add(15, "Transport Owner");
				lParamList.add(16, "Transport Owner");
				lParamList.add(17, tit.getBankName());
				lParamList.add(18, tit.getAccountNo());
				
				lParamList.add(19, tit.getIfscCode());
				lParamList.add(20, tit.getMicrCode());
				
				lParamList.add(21, null);
				
				lParamList.add(22, tit.getMulFactor());
				lParamList.add(23, tit.getLandingCost());
				lParamList.add(24, tit.getExtraField1());
				lParamList.add(25, null);
				
				// Get the query by passing parameters
				String query = getQuery(QueryConstant.SAVE_TRADER, lParamList);
				int resultCount = insert(query);
				if (resultCount != 0) {
					isSaved = true;
				} else
					isSaved = false;
				closeConnection();

			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.SAVE_TRADER_FAILED);
			}
		}
		return isSaved;
	}

	public List<Tistit> getAllTraders() throws ApplicationException {

		List<Tistit> list_traders = new ArrayList<Tistit>();
		List lParamList = new ArrayList();
		lParamList.add(0, DBConstant.IS_ACTIVE_YES);

		String query = getQuery(QueryConstant.ALL_TRADERS, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				Tistit tit = new Tistit();
				tit.setCompanyId(rs.getInt("COMPANY_ID"));
				tit.setCompanyName(rs.getString("COMPANY_NAME"));
				tit.setContactNo(rs.getString("CONTACT_NO"));
				tit.setAddress(rs.getString("STREET_ADDRESS"));
				tit.setEmailId(rs.getString("EMAIL_ID"));
				tit.setCreateDate(rs.getDate("CREATED_DT"));
				tit.setDesc(rs.getString("DISCRIPTION"));
				
				tit.setMobileNo(rs.getString("MOBILE_NO"));
				tit.setPanNo(rs.getString("PAN_NO"));
				
				tit.setBankName(rs.getString("BANK_NAME"));
				tit.setAccountNo(rs.getString("ACCOUNT_NO"));
				
				tit.setIfscCode(rs.getString("IFSC_CODE"));
				tit.setMicrCode(rs.getString("MICR_CODE"));
								
				list_traders.add(tit);
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.TRADERS_NOT_IN_SYSTEM);
		}
		return list_traders;
	}

	public boolean deleteTradersOnId(String CompanyId)
			throws ApplicationException {
		boolean isDeleted = false;
		List<String> lParamList = new ArrayList<String>();
		lParamList.add(0, DBConstant.IS_ACTIVE_NO);
		lParamList.add(1, CompanyId);
		String query = getQuery(QueryConstant.deleteTrader, lParamList);
		int resultCount = 0;
		try {
			resultCount = delete(query);
			if (resultCount != 0) {
				isDeleted = true;
			} else
				isDeleted = false;
		} catch (SQLException sqe) {
			throw new ApplicationException(ExceptionMessage.TRADER_NOT_DELETED);
		}
		return isDeleted;
	}

	public List<Tistit> getTradersOnId(String companyId)
			throws ApplicationException {

		List<Tistit> list_traders = new ArrayList<Tistit>();
		List lParamList = new ArrayList();
		lParamList.add(0, companyId);

		String query = getQuery(QueryConstant.GET_TRADERS_ON_ID, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				Tistit tit = new Tistit();
				tit.setCompanyId(rs.getInt("COMPANY_ID"));
				tit.setCompanyName(rs.getString("COMPANY_NAME"));
				tit.setContactNo(rs.getString("CONTACT_NO"));
				tit.setAddress(rs.getString("STREET_ADDRESS"));
				tit.setEmailId(rs.getString("EMAIL_ID"));
				tit.setCreateDate(rs.getDate("CREATED_DT"));
				tit.setDesc(rs.getString("DISCRIPTION"));
				
				tit.setMobileNo(rs.getString("MOBILE_NO"));
				tit.setPanNo(rs.getString("PAN_NO"));
				
				tit.setBankName(rs.getString("BANK_NAME"));
				tit.setAccountNo(rs.getString("ACCOUNT_NO"));
				
				tit.setIfscCode(rs.getString("IFSC_CODE"));
				tit.setMicrCode(rs.getString("MICR_CODE"));
				tit.setMulFactor(rs.getString("MULTIPLY_FACTOR"));
				tit.setLandingCost(rs.getString("LANDING_COST"));
				tit.setExtraField1(rs.getString("EXTRA_FIELD1"));
				list_traders.add(tit);
			}
		} catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.TRADERS_NOT_IN_SYSTEM);
		}
		return list_traders;
	}

	public boolean updateTrader(Tistit tit) throws ApplicationException {
		// Prepare the parameters
		List lParamList = new ArrayList();
		boolean isupdated = false;
		if (tit != null) {
			try {
				lParamList.add(0, "" + tit.getCompanyId());
				lParamList.add(1, tit.getCompanyName());
				lParamList.add(2, tit.getAddress());
				lParamList.add(3, tit.getContactNo());
				lParamList.add(4, tit.getEmailId());
				lParamList.add(5, tit.getDesc());
				lParamList.add(6, new Date(System.currentTimeMillis()));

				lParamList.add(7, tit.getMobileNo());
				lParamList.add(8, tit.getPanNo());
				
				lParamList.add(9, tit.getBankName());
				lParamList.add(10, tit.getAccountNo());
				
				lParamList.add(11, tit.getIfscCode());
				lParamList.add(12, tit.getMicrCode());
				lParamList.add(13, tit.getMulFactor());
				lParamList.add(14, tit.getLandingCost());
				lParamList.add(15, tit.getExtraField1());
				
				// Get the query by passing parameters
				String query = getQuery(QueryConstant.UPDATE_TRADER, lParamList);
				int resultCount = insert(query);
				if (resultCount != 0) {
					isupdated = true;
				} else
					isupdated = false;
				closeConnection();

			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.TRADER_NOT_SAVED);
			}
		}
		return isupdated;
	}
}
